package ar.com.huargo.easyio.io;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 
 * @author Augusto Recordon		-CeSPI-UNLP-
 * 
 * Esta clase provee una interfaz de lectura de archivos
 * simple y directa.
 *
 */
public class FileReader {
	
	private String fileName;
	
	private FileInputStream fis;
	
	private DataInputStream dis;
	
	private BufferedReader br;
	
	private String line;
	
	public FileReader(String fileName){
		super();
		this.fileName = fileName;
	}
	
	public void open() throws FileNotFoundException{
		this.fis = new FileInputStream(this.fileName);
		this.dis = new DataInputStream(this.fis);
		this.br = new BufferedReader(new InputStreamReader(this.dis));
		this.line = "";
	}
	
	public String readLine() throws IOException{
		return this.line;
	}
	
	public boolean eof() throws IOException{
		if(this.line != null){
			this.line = this.br.readLine();
		}
		return this.line == null;
	}
	
	
	public void close() throws IOException{
		this.dis.close();
		this.br.close();
		this.line = null;
	}
	
	public String[] splitLine(String regex){
		return this.line.split(regex);
	}
	
	
	public void reset() throws IOException{
		this.close();
		this.open();
	}
	
	
	public static boolean exists(String fileName){
		File f = new File(fileName);
	    return f.exists();
	}
	
}
